<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成不重复的随机数数组</title>
</head>

<body>
    <script>
        let arr = new Array(5)
        let i = 0
        let num = getRandomNum()

        function getRandomNum() {
            return Math.floor(Math.random() * 31 + 2)
        }

        function getRandomArr(arr, num) {
            if (arr.indexOf(num) < 0) {
                arr[i++] = num
            } else {
                num = getRandomNum()
            }
            if (arr.length > i) {
                getRandomArr(arr, num)
            } else {
                console.log(arr)
                return
            }
        }
        getRandomArr(arr, num)
    </script>
</body>

</html>